package ModelLayer;

import java.util.ArrayList;

/**
 * Write a description of class PersonContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonContainer
{
    private static PersonContainer instance;
    private Person person;
    private ArrayList<Person> people;

    /**
     * Constructor for objects of class PersonContainer
     */
    public PersonContainer()
    {
        people = new ArrayList<Person>();
    }

    public static PersonContainer getInstance()
    {
        if(instance == null)
        {
            instance = new PersonContainer();
        }
        return instance;
    }

    public boolean addPerson(Person newPerson)
    {
        boolean added = false;
        if(findPerson(newPerson.getPersonID()) == null)
        {
            people.add(newPerson);
            added = true;
        }
        return added;
    }

    public Person findPerson(int id)
    {
        person = null;
        for(int i=0; i<people.size(); i++)
        {
            if(people.get(i).getPersonID() == id)
            {
                person = people.get(i);
            }
        }

        return person;
    }

    public Person findPersonByName(String name)
    {
        for(int i=0; i<people.size(); i++)
        {
            if(people.get(i).getName().equals(name))
            {
                person = people.get(i);
            }
        }

        if(person != null){
            return person;
        } 
        else 
        {
            System.out.println("There is no such person!");
            return null;
        }
    }

    public void deletePerson(int id)
    {
        if(people.contains(findPerson(id))){
            people.remove(findPerson(id));
            System.out.println("The person has been deleted!");
        }
        else
        {
            System.out.println("There is no such person!");
        }
    }
}
